package com.wonders.hs.pd.mm.web;

import com.wonders.hs.pd.mm.web.utils.JDBCConnect;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.util.*;

/**
 * @author banglong
 * @date 2017/5/3
 */
public class ReportQuery {
  @Test
  public void test() {
    List<Map<String, String>> mapList = JDBCConnect.medicareCodeQuery("2017-01-01", "2017-05-30", 100);
    // 结果数据
    List<Map<String, String>> result = new ArrayList<>();
    String supplierName = ""; // 供应商名称
    int index = 1;
    // 循环处理结果
    for (int i = 0; i < mapList.size(); i++, index++) {
      // 获取结果
      LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) mapList.get(i);
      // 第一次时候需要加入
      if (i == 0) {
        result.add(newTitle(map));
      }
      // 判断供应商是否为空
      if (StringUtils.isEmpty(supplierName)) {
        supplierName = map.get("供应商名称");
      }
      // 判断供应商是否是上次的供应商 如果不是 重新开始 加入 title
      if (!supplierName.equals(map.get("供应商名称"))) {
        index = 1;
        supplierName = map.get("供应商名称");
        result.add(newTitle(map));
      }
      // 加入序号
      map.put("序号", String.valueOf(index));
      result.add(map);
    }

    StringBuilder builder = new StringBuilder();
    // 序号需要放在第一位
    if (mapList.size() > 0) {
      builder.append("序号,");
      Map<String, String> map = mapList.get(0);
      for (Map.Entry<String, String> entry : map.entrySet()) {
        if (!"序号".equals(entry.getKey())) {
          builder.append(entry.getKey());
          builder.append(",");
        }
      }
    }
    System.out.println(builder.toString().substring(0, builder.toString().length() - 1));
  }

  private Map<String, String> newTitle(Map<String, String> map) {
    Map<String, String> titleMap = new HashMap<>();
    // title 标识
    titleMap.put("title", "title");
    titleMap.put("供应商名称", map.get("供应商名称"));
    titleMap.put("供应商联系人", map.get("供应商联系人"));
    titleMap.put("供应商联系电话", map.get("供应商联系电话"));
    return titleMap;
  }

  @Test
  public void testBillQuery() {
    List<Map<String, String>> result = JDBCConnect.billQuery("17052600000061");
    System.out.println(result);
  }
}
